package io.framework.modules.desktop.controller;

import io.framework.common.constant.Constant;
import io.framework.common.page.PageData;
import io.framework.common.utils.Result;
import io.framework.modules.air.dto.AirDTO;
import io.framework.modules.air.service.AirService;
import io.framework.modules.hpinfo.dto.HpglProjectGoordInfoDTO;
import io.framework.modules.hpinfo.service.VTHpglProjectinfoService;
import io.framework.modules.noise.service.NoiseFunctionalZoneService;
import io.framework.modules.noise.service.NoiseMonitorService;
import io.framework.modules.oss.entity.SysOssEntity;
import io.framework.modules.oss.service.SysOssService;
import io.framework.modules.pollutionsource.dto.PollutionGoordInfoDTO;
import io.framework.modules.pollutionsource.dto.PollutionListDTO;
import io.framework.modules.pollutionsource.dto.PollutionlabeldirGoordInfoDTO;
import io.framework.modules.pollutionsource.service.TBasPollutionService;
import io.framework.modules.pollutionsource.service.TBasPollutionlabeldirService;
import io.framework.modules.soil.dto.SoilDTO;
import io.framework.modules.soil.enums.SoilTypeEnum;
import io.framework.modules.soil.service.SoilService;
import io.framework.modules.station.dto.StationDTO;
import io.framework.modules.station.service.StationService;
import io.framework.modules.water.dto.OceanBuoyPointDTO;
import io.framework.modules.water.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 一张图
 *
 * @author gujq
 * @date
 */
@RestController
@Api(tags = "一张图接口")
public class DataController {

    /**
     * 污染源监控基本信息 一般污染源
     */
    @Autowired
    private TBasPollutionService basPollutionService;

    /**
     * 污染源标签年份表名录 （重点排污单位，环统单位）
     */
    @Autowired
    private TBasPollutionlabeldirService basPollutionlabeldirService;
    /**
     * 环评建设项目信息
     */
    @Autowired
    private VTHpglProjectinfoService vTHpglProjectinfoService;
    /**
     * 站点信息
     */
    @Autowired
    private StationService stationService;
    /**
     * 空气站点监测数据
     */
    @Autowired
    private AirService airService;
    /**
     * 土壤
     */
    @Autowired
    private SoilService soilService;
    /**
     * 水环境
     */
    @Autowired
    private WaterService waterService;
    /**
     * 水环境 城镇内河(湖)
     */
    @Autowired
    private WaterMonitorService waterMonitorService;
    /**
     * 水环境 城镇内河(湖)
     */
    @Autowired
    private WaterCityRiverService waterCityRiverService;
    /**
     * 水环境 近岸海域海水
     */
    @Autowired
    private WaterNearshoreService waterNearshoreService;
    /**
     * 水环境 滨海旅游区海水
     */
    @Autowired
    private WaterBinhaiService waterBinhaiService;
    /**
     * 道路交通/区域噪声管理
     */
    @Autowired
    private NoiseMonitorService noiseMonitorService;
    /**
     * 功能区噪声管理
     */
    @Autowired
    private NoiseFunctionalZoneService noiseFunctionalZoneService;
    /**
     * 海洋浮标点信息
     */
    @Autowired
    private OceanBuoyPointService oceanBuoyPointService;
    /**
     * 其他系统 列表
     */
    @Autowired
    private SysOssService sysOssService;

    @GetMapping("data/pollution/all")
    @ApiOperation("一张图-污染源 坐标信息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "regionName", value = "区域  （多个以英文逗号“,”隔开）", paramType = "query", required = false, dataType = "string"),
            @ApiImplicitParam(name = "attentionDegreeName", value = "关注程度  县控  市控  省控  国控 其他   （多个以英文逗号“,”隔开）", paramType = "query", required = false, dataType = "string"),
    })
    public Result<PollutionListDTO> pollutionAll(@ApiIgnore @RequestParam Map<String, Object> params) {
        PollutionListDTO pollutionListDTO = new PollutionListDTO();

        List<PollutionGoordInfoDTO> pollutionGoordInfoDTOS = basPollutionService.getCoordList(params);
        List<PollutionlabeldirGoordInfoDTO> pollutionlabeldirGoordInfoDTOS = basPollutionlabeldirService.getCoordList(params);

        pollutionListDTO.setAttentionWRYGoordList(pollutionlabeldirGoordInfoDTOS);
        pollutionListDTO.setGeneralWRYGoordList(pollutionGoordInfoDTOS);

        return new Result<PollutionListDTO>().ok(pollutionListDTO);
    }

    @GetMapping("data/hpinfo/vthpglprojectinfo/list")
    @ApiOperation("一张图-环评信息   环评坐标列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectXmmc", value = "项目名称", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "pffsName", value = "排放方式   直接排放、间接排放、不排放    （多个以英文逗号”,“隔开）", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "ifXkz", value = "是否有许可证  是  否 ", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "ifWry", value = "是否有污染源  是  否 ", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "hpspbm", value = "环评审批部门", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "regionName", value = "区域   天涯区  吉阳区 海棠区 崖州区   （多个以英文逗号”,“隔开）", paramType = "query", dataType = "String"),
    })
    public Result<List<HpglProjectGoordInfoDTO>> getCoordList(@ApiIgnore @RequestParam Map<String, Object> params) {

        List<HpglProjectGoordInfoDTO> list = vTHpglProjectinfoService.getCoordList(params);

        return new Result<List<HpglProjectGoordInfoDTO>>().ok(list);
    }

    @GetMapping("data/hpinfo/vthpglprojectinfo/statistics")
    @ApiOperation("一张图-环评信息   环评类型统计")
    public Result<List<Map<String, Object>>> statistics() {

        List<Map<String, Object>> list = vTHpglProjectinfoService.statistics();

        return new Result<List<Map<String, Object>>>().ok(list);
    }

    @GetMapping("data/pollution/TypeCount")
    @ApiOperation("一张图-污染源 污染源类型数量")
    public Result<List<Map<String, Object>>> wryRegionTypeCount(@ApiIgnore @RequestParam Map<String, Object> params) {

        List<Map<String, Object>> mapList = basPollutionService.getWryTypeCount();

        return new Result<List<Map<String, Object>>>().ok(mapList);
    }

    @GetMapping("data/pollution/RegionCount")
    @ApiOperation("一张图-污染源 污染源各区情况")
    public Result<List<Map<String, Object>>> wryRegionCount(@ApiIgnore @RequestParam Map<String, Object> params) {

        List<Map<String, Object>> list = basPollutionService.getRegionCount();

        return new Result<List<Map<String, Object>>>().ok(list);
    }

    @GetMapping("data/stationinfo/list")
    @ApiOperation("一张图-各站点信息分布情况")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "站点名称", paramType = "query", required = true),
            @ApiImplicitParam(name = "control", value = "空气：1-国控 2-市控  水：1-地表水 2-城镇内河 3-重点治理城镇内河 4-饮用水源地", paramType = "query", required = true),
            @ApiImplicitParam(name = "type", value = "1-空气 2-水 3-土壤 4-噪声", paramType = "query", required = true),
            @ApiImplicitParam(name = "xmin", value = "最小经度", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "xmax", value = "最大经度", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "ymin", value = "最小纬度", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "ymax", value = "最大纬度", paramType = "query", dataType = "String"),
    })
    public Result<List<StationDTO>> getStationInfoList(@ApiIgnore @RequestParam Map<String, Object> params) {

        List<StationDTO> list = stationService.getStationInfoByParams(params);
        list.forEach(v -> {
            if (v.getType() == 1) {
                //空气
                Map<String, Object> map = new HashMap<>();
                map.put("type", v.getType());
                map.put("stationId", v.getId());
                AirDTO airDTO = airService.getAirDTOIOnfoByParam(map);
                if (airDTO != null) {
                    v.setLevel(airDTO.getLevel());
                    v.setClassify(airDTO.getClassify());
                }
            }
        });
        return new Result<List<StationDTO>>().ok(list);
    }

    @GetMapping("data/air/stationinfo/airQuality")
    @ApiOperation("一张图-全市空气质量概括")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stationId", value = "站点ID", paramType = "query", required = true),
            /*@ApiImplicitParam(name = "type", value = "1-国控  2-全市", paramType = "query", required = true),*/
    })
    public Result<AirDTO> getAirStationInfoByParams(@ApiIgnore @RequestParam Map<String, Object> params) {
        //需求更改， 需要显示所有站点信息
        /*Integer type = Integer.parseInt(params.get("type").toString());
        if (type == 2) {
            //type=3 全市数据
            params.put("type", 3);
        }*/
        AirDTO airDTO = airService.getAirDTOIOnfoByParam(params);

        return new Result<AirDTO>().ok(airDTO);
    }

    @GetMapping("data/air/stationinfo/sort")
    @ApiOperation("一张图-全市空气站点监测排名")
    public Result<List<Map<String, Object>>> getAirStationInfoSort(@ApiIgnore @RequestParam Map<String, Object> params) {

        List<Map<String, Object>> airDTOList = airService.getAirStationInfoSort(params);

        return new Result<List<Map<String, Object>>>().ok(airDTOList);
    }

    @GetMapping("data/air/stationinfo/info")
    @ApiOperation("一张图-查询空气站点实时数据信息 站点详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stationId", value = "站点Id", paramType = "query", required = true),
    })
    public Result<AirDTO> getAirStationInfoById(@ApiIgnore @RequestParam Map<String, Object> params) {

        AirDTO airDTO = airService.getAirDTOIOnfoByParams(params);

        return new Result<AirDTO>().ok(airDTO);
    }

    /*@GetMapping("data/air/stationinfo/info")
    @ApiOperation("一张图-查询空气站点实时数据信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "address", value = "请求接口", paramType = "query", required = true),
    })
    public Result<Map<String, Object>> getAirInfo(@ApiIgnore @RequestParam Map<String, Object> params) {

        Map<String, Object> map = AirMonitorUtil.getAirMinitorInfo(params);

        return new Result<Map<String, Object>>().ok(map);
    }*/

    @GetMapping("data/air/stationinfo/historyInfo/page")
    @ApiOperation("一张图-查询空气站点历史数据信息 --分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = "stationId", value = "站点Id", paramType = "query", required = true),
            @ApiImplicitParam(name = "startDate", value = "开始时间", paramType = "query"),
            @ApiImplicitParam(name = "endDate", value = "结束时间", paramType = "query"),
    })
    public Result<PageData<AirDTO>> getStationInfoById(@ApiIgnore @RequestParam Map<String, Object> params) {

        PageData<AirDTO> page = airService.page(params);

        return new Result<PageData<AirDTO>>().ok(page);
    }

    @GetMapping("data/soil/info/pass")
    @ApiOperation("一张图-土壤达标情况")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "1-居民区周边土壤 2-基本农田 3-畜牧养殖周边土壤 4-园地 5-饮用水源地 6-生活垃圾场周边土壤", paramType = "query", dataType = "int", required = true),
            @ApiImplicitParam(name = "date", value = "时间 2019", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "pass", value = "1-达标 2-超标", paramType = "query", dataType = "int", required = true),
    })
    public Result<List<Map<String, Object>>> pass(@ApiIgnore @RequestParam Map<String, Object> params) {
        Integer type = Integer.parseInt(params.get("type").toString());
        params.put("name", SoilTypeEnum.getTypeNameByType(type));

        Integer pass = Integer.parseInt(params.get("pass").toString());
        if (pass == 1) {
            params.put("pass", "达标");
        } else {
            params.put("pass", "不达标");
        }
        List<Map<String, Object>> mapList = soilService.getPassInfo(params);

        return new Result<List<Map<String, Object>>>().ok(mapList);
    }

    @GetMapping("data/soil/stationinfo/getSoilInfoById")
    @ApiOperation("一张图-土壤站点详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stationId", value = "站点Id", paramType = "query", required = true),
    })
    public Result<List<SoilDTO>> getSoilStationInfoById(@ApiIgnore @RequestParam Map<String, Object> params) {

        List<SoilDTO> list = soilService.getSoilStationInfoById(params);

        return new Result<List<SoilDTO>>().ok(list);
    }

    @GetMapping("soil/info/getVillagePassRate")
    @ApiOperation("一张图-土壤 达标情况排名")
    public Result<List<Map<String, Object>>> getVillagePassRate(@ApiIgnore @RequestParam Map<String, Object> params) {

        List<Map<String, Object>> mapList = soilService.getPassNumSort(params);

        return new Result<List<Map<String, Object>>>().ok(mapList);
    }

    @GetMapping("data/water/info/pass")
    @ApiOperation("一张图-水环境 水质达标情况")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "1-地表水 2-城镇内河 3-重点治理城镇内河（湖） 4-饮用水源  5-海洋浮标点", paramType = "query", dataType = "String", required = true),
    })
    public Result<Map<String, Object>> waterPass(@ApiIgnore @RequestParam Map<String, Object> params) {

        Map<String, Object> resultMap = waterService.getWaterPassInfo(params);

        return new Result<Map<String, Object>>().ok(resultMap);
    }

    @GetMapping("data/water/info/rate")
    @ApiOperation("一张图-水环境 站点水质现状类别占比")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "1-地表水 2-城镇内河 3-重点治理城镇内河（湖） 4-饮用水源", paramType = "query", dataType = "String", required = true),
    })
    public Result<Map<String, Object>> waterRate(@ApiIgnore @RequestParam Map<String, Object> params) {

        Map<String, Object> map = waterService.getwaterRateInfo(params);

        return new Result<Map<String, Object>>().ok(map);
    }

    @GetMapping("data/water/info/getWaterInfoById")
    @ApiOperation("一张图-水环境 单个站点数据详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "1-地表水 2-城镇内河 3-重点治理城镇内河（湖） 4-饮用水源", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "stationId", value = "站点id", paramType = "query", dataType = "String", required = true),
    })
    public Result<Object> getWaterInfoById(@ApiIgnore @RequestParam Map<String, Object> params) {
        Integer type = Integer.parseInt(params.get("type").toString());
        Object obj = null;
        if (type == 2) {
            obj = waterMonitorService.getWaterInfoById(params);
        } else if (type == 3) {
            obj = waterCityRiverService.getWaterInfoById(params);
        } else if (type == 1 || type == 4) {
            obj = waterService.getWaterInfoByStationId(params);
        } else if (type == 6) {
            obj = waterNearshoreService.getWaterInfoById(params);
        } else if (type == 7) {
            obj = waterBinhaiService.getWaterInfoById(params);
        }
        return new Result<>().ok(obj);
    }

    @GetMapping("data/noise/noisemonitor/info")
    @ApiOperation("一张图-噪声管理 --单个站点数据详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "1-昼间 2-夜间", paramType = "query", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "stationId", value = "监测站点", paramType = "query", dataType = "Long", required = true),
            @ApiImplicitParam(name = "classify", value = "1-道路交通噪声 2-区域噪声 3-功能区噪声", paramType = "query", dataType = "Long", required = true),
    })
    public Result<Object> noiseInfo(@ApiIgnore @RequestParam Map<String, Object> params) {

        Object obj = null;
        Integer classify = Integer.parseInt(params.get("classify").toString());
        if (classify == 3) {
            obj = noiseFunctionalZoneService.getNoiseInfo(params);
        } else {
            obj = noiseMonitorService.getNoiseInfo(params);
        }
        return new Result<Object>().ok(obj);
    }

    @GetMapping("data/noise/noisemonitor/passInfo")
    @ApiOperation("一张图-噪声管理 --功能区噪声达标情况")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "1-昼间 2-夜间", paramType = "query", dataType = "Integer", required = true),
    })
    public Result<Map<String, Object>> passInfoGGQ(@ApiIgnore @RequestParam Map<String, Object> params) {

        Map<String, Object> map = new HashMap<>(2);
        List<Map<String, Object>> passNum = noiseFunctionalZoneService.pass(params);
        List<Map<String, Object>> passRate = noiseFunctionalZoneService.passNum(params);
        map.put("noisePassNum", passNum);
        map.put("noisePassRate", passRate);
        //createDate
        map.putAll(noiseFunctionalZoneService.getNewCreateDate());

        return new Result<Map<String, Object>>().ok(map);
    }

    @GetMapping("data/noise/noisemonitor/dataInfo")
    @ApiOperation("一张图-噪声管理 --道路交通噪声/区域噪声数据情况")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "classify", value = "1-道路交通噪声 2-区域噪声 3-功能区噪声", paramType = "query", dataType = "Long", required = true),
            @ApiImplicitParam(name = "type", value = "1-昼间 2-夜间", paramType = "query", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "stationId", value = "监测站点", paramType = "query", dataType = "Long", required = true),
    })
    public Result<List<Map<String, Object>>> dataInfo(@ApiIgnore @RequestParam Map<String, Object> params) {

        List<Map<String, Object>> mapList = noiseMonitorService.statistics(params);

        return new Result<List<Map<String, Object>>>().ok(mapList);
    }

    @GetMapping("data/noise/noisemonitor/dataInfo_new")
    @ApiOperation("一张图-噪声管理 --道路交通噪声/区域噪声数据情况")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "classify", value = "1-道路交通噪声 2-区域噪声", paramType = "query", dataType = "Long", required = true),
            @ApiImplicitParam(name = "stationId", value = "监测站点", paramType = "query", dataType = "Long", required = false),
            @ApiImplicitParam(name = "time", value = "监测年份（例：2020）", paramType = "query", dataType = "Long", required = false),
    })
    public Result<Map<String, Object>> dataInfo_new(@ApiIgnore @RequestParam Map<String, Object> params) {

        Map<String, Object> map = noiseMonitorService.statistics_new(params);

        return new Result<Map<String, Object>>().ok(map);
    }

    @ApiOperation("一张图-水环境 获取海洋浮标点单个详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stationCode", value = "站点编码", paramType = "query", dataType = "String", required = true)
    })
    @GetMapping("data/water/ocean/getOceanPointInfoByCode")
    public Result<OceanBuoyPointDTO> getOceanPointInfoByCode(@ApiIgnore @RequestParam Map<String, Object> params) {

        OceanBuoyPointDTO dto = oceanBuoyPointService.getOceanInfoByStationCode(params.get("stationCode").toString());

        return new Result<OceanBuoyPointDTO>().ok(dto);
    }

    @GetMapping("data/oss/list")
    public Result<List<SysOssEntity>> list(@ApiIgnore @RequestParam Map<String, Object> params) {

        List<SysOssEntity> list = sysOssService.getList(params);

        return new Result<List<SysOssEntity>>().ok(list);
    }
}
